package VLSys;

import java.awt.*;
import javax.swing.*;

import DBConnection.*;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.sql.*;

import javax.swing.table.DefaultTableModel;
import javax.swing.table.JTableHeader;
import javax.swing.table.TableColumnModel;
import javax.swing.table.TableModel;
import javax.swing.table.TableRowSorter;


public class ReportsServices extends JPanel{
    JLabel serviceList;
    
    JScrollPane serviceScroll;
    JTable serviceTable;
    
    JTextField filter;
    JLabel filterLabel;
    
    DefaultTableModel model;
    
    RowSorter restore;
    


   
    public ReportsServices(final JFrame frame)
    {   setVisible(false);
        setLayout(null);
        setBackground(null);
        
        
        serviceList = new JLabel("SERVICES");
        serviceList.setFont(new Font("sansserif", Font.PLAIN, 40));
        serviceList.setBounds(10,10,300,30);
        
        filterLabel = new JLabel("Search:");
        filterLabel.setBounds(394,400,150,20);
        
        filter = new JTextField();
        filter.setBounds(439,400,150,20);
        filter.setBorder(null);
        
        //table codes
        Object headers[] = {"ServiceID", "Service Name", "Size", "Amount", "Available"};

        serviceTable = new JTable();
        serviceTable.setModel(new javax.swing.table.DefaultTableModel(null, headers
        ) {


            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return false;
            }
        });
        serviceScroll = new JScrollPane();
        serviceTable.setAutoCreateRowSorter(true);
        serviceTable.getTableHeader().setReorderingAllowed(false);
        serviceScroll.setViewportView(serviceTable);
        serviceScroll.setBounds(15, 50, 575, 345);
        
        model = (DefaultTableModel)serviceTable.getModel();
        
        serviceTable.getTableHeader().setResizingAllowed(false);
        

        
          filter.addKeyListener(new KeyAdapter() {
          public void keyReleased(KeyEvent e) {
              
              if (e.getKeyCode()==39 || e.getKeyCode()==37)
        {
               
        }
              else 
                filter.setText(capitalizeFirstLetters(filter.getText()));
     
          

          populateTable("SELECT * FROM servicelist WHERE serviceName LIKE '" + filter.getText() + "%' ORDER BY serviceName ASC");
          
          }

          public void keyTyped(KeyEvent e) {
          }

          public void keyPressed(KeyEvent e) {
          }
        });
        
        add(serviceList);
        add(serviceScroll);
        add(filter);
        add(filterLabel);
        
        
    }
    
    public boolean populateTable(String pS){
         boolean result = false;
              try{
                    //opens db connection
                    ConnectionFactory myFactory = ConnectionFactory.getFactory();
                    Connection conn = myFactory.getConnection();

                    //SQL Query
                    PreparedStatement pstmt = conn.prepareStatement(pS);

                    //execute query
                    ResultSet rs = pstmt.executeQuery();
                    
                    model.getDataVector().removeAllElements();
                    serviceTable.setAutoCreateRowSorter(true);
                    //populate JTable 
                    while(rs.next()){
                    model.addRow(new Object[]{rs.getInt(1), rs.getString(2), rs.getString(3),rs.getString(4), rs.getString(5)});
                    model.fireTableDataChanged();
                    }
                    
                    //close DB connection
                    conn.close();
                    
                    
                }
                      
                catch(SQLException e)
                {
                   e.printStackTrace();
                   System.out.print("ayaw!");
                }
              
              return result;
             
          }
    
    public static String capitalizeFirstLetters ( String s ) {

        for (int i = 0; i < s.length(); i++) {

            if (i == 0) {
                // Capitalize the first letter of the string.
                s = String.format( "%s%s",
                             Character.toUpperCase(s.charAt(0)),
                             s.substring(1) );
            }

            // Is this character a non-letter or non-digit?  If so
            // then this is probably a word boundary so let's capitalize
            // the next character in the sequence.
            if (!Character.isLetterOrDigit(s.charAt(i))) {
                if (i + 1 < s.length()) {
                    s = String.format( "%s%s%s",
                                 s.subSequence(0, i+1),
                                 Character.toUpperCase(s.charAt(i + 1)),
                                 s.substring(i+2) );
                }
            }

        }

        return s;

    }
    
    
    
       
    
    
}
